package org.xyl.context;

import java.util.*;
import java.util.regex.*;

/**
 * 实体识别器 - 负责识别用户输入中的业务实体
 */
public class EntityRecognizer {
    
    // 实体类型常量
    public static final String ENTITY_DEVICE_ID = "device_id";
    public static final String ENTITY_DEVICE_MODEL = "device_model";
    public static final String ENTITY_CUSTOMER = "customer";
    public static final String ENTITY_WORKORDER = "workorder";
    public static final String ENTITY_ALERT = "alert";
    
    // 设备ID正则表达式模式 (示例)
    private static final Pattern DEVICE_ID_PATTERN = Pattern.compile("A\\d{3}");
    
    // 设备型号正则表达式模式 (示例)
    private static final Pattern DEVICE_MODEL_PATTERN = Pattern.compile("ESL-\\d+");
    
    /**
     * 从用户查询中识别实体
     */
    public Map<String, List<String>> recognizeEntities(String userQuery) {
        Map<String, List<String>> entities = new HashMap<>();
        
        // 识别设备ID
        recognizeDeviceIds(userQuery, entities);
        
        // 识别设备型号
        recognizeDeviceModels(userQuery, entities);
        
        // TODO: 扩展更多实体识别逻辑
        
        return entities;
    }
    
    private void recognizeDeviceIds(String text, Map<String, List<String>> entities) {
        Matcher matcher = DEVICE_ID_PATTERN.matcher(text);
        List<String> deviceIds = new ArrayList<>();
        
        while (matcher.find()) {
            deviceIds.add(matcher.group());
        }
        
        if (!deviceIds.isEmpty()) {
            entities.put(ENTITY_DEVICE_ID, deviceIds);
        }
    }
    
    private void recognizeDeviceModels(String text, Map<String, List<String>> entities) {
        Matcher matcher = DEVICE_MODEL_PATTERN.matcher(text);
        List<String> models = new ArrayList<>();
        
        while (matcher.find()) {
            models.add(matcher.group());
        }
        
        if (!models.isEmpty()) {
            entities.put(ENTITY_DEVICE_MODEL, models);
        }
    }
}